package com.datn.activity;

import java.util.ArrayList;
import java.util.List;

import com.datn.DataAccess.ListPlaceAdapter;
import com.datn.utils.AlertCustom;
import com.datn.utils.BitmapUtils;
import com.datn.utils.GPSUtils;
import com.datn.utils.HelloItemizedOverlay;
import com.datn.utils.MyOverlay;
import com.datn.utils.PreferenceUtils;
import com.datn.utils.places.DirectionObject;
import com.datn.utils.places.JsonDirection;
import com.datn.utils.places.JsonPlace;
import com.datn.utils.places.Place;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ListView;
import android.widget.Toast;

public class DirectionOnMapActivity extends MapActivity{

	private MapView mapView;
	private List<Overlay> lstOverlay;
	private ArrayList<GeoPoint> listDetailsPosition;
	private GeoPoint startPosition,endPosition;
	private int zoomLevel=14;
	private Place placeReceived;
	private Context mContext;
	
	public GeoPoint getValueFromOtherActivity(){
		placeReceived = PreferenceUtils.getPlaceDetail();
		double latitude=0;
		double longitude=0;

		if(placeReceived !=null) {
			latitude = placeReceived.getLatitude();
			longitude = placeReceived.getLongtitude();
		}
		Log.v("DirectionONMap", "Lat:"+placeReceived.getLatitude() + "  Lng:"+placeReceived.getLongtitude());
		return new GeoPoint((int)(latitude*1E6), (int)(longitude*1E6));
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.direction_view);
		mContext = this;
		PreferenceUtils.getInstance(this);
		
		
		try {


			
			
			//Start and End Location
			startPosition=PreferenceUtils.getCurrentLocation();
			endPosition= getValueFromOtherActivity();


			updateListGeoPointLocation(startPosition, endPosition);
			
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			AlertCustom.showAlert(this, "Error", e.getMessage());
		}
	}

	public HelloItemizedOverlay drawMarkerOnMap(GeoPoint gp, int resourceView, String stInfomationPopup){
		Drawable startMarker = this.getResources().getDrawable(resourceView);
		HelloItemizedOverlay startItemOverlay = new HelloItemizedOverlay(startMarker, this);
		OverlayItem overlayitemstart = new OverlayItem(gp, "Infomation Place", stInfomationPopup);
		startItemOverlay.addOverlay(overlayitemstart);
		return startItemOverlay;
	}





	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		if (keyCode==KeyEvent.KEYCODE_BACK){
			Intent intent=new Intent(this, DetailsPlaceActivity.class);
			startActivity(intent);
			finish();
		}		
		return super.onKeyDown(keyCode, event);
	}

	
	
	public void updateListGeoPointLocation(final GeoPoint startLocation, final GeoPoint endLocation){
		new AsyncTask<String, Void, ArrayList<GeoPoint>>() 
		{
			private ProgressDialog mProgressDialog;
			@Override
			protected void onPreExecute() 
			{
				mProgressDialog = new ProgressDialog(mContext);
				mProgressDialog.setMessage("Please Wait...");
				mProgressDialog.setCancelable(false);
				mProgressDialog.setIndeterminate(true);
				mProgressDialog.show();
			};

			@Override
			protected ArrayList<GeoPoint> doInBackground(String... args) {
				// TODO Auto-generated method stub
				try {
					//JsonDirection jsonDirection=new JsonDirection(new GeoPoint(16074785,108171923), new GeoPoint(16065754,108194325));
					JsonDirection jsonDirection=new JsonDirection(mContext, startLocation, endLocation);
					DirectionObject direction = jsonDirection.getDirectionFromJSON();

					//Get details direction
					listDetailsPosition= direction.getPolyline();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();

				}
				return listDetailsPosition;
			}

			@Override
			protected void onPostExecute(ArrayList<GeoPoint> array) 
			{
				if (mProgressDialog.isShowing()) 
				{
					mProgressDialog.dismiss();
				}
				if (array != null) 
					{
						updateListToMapView();
					} 
				else 
					{
						/*AlertCustom.showAlert(mContext, "Error", "Cannot receive direction");*/
						Toast.makeText(mContext, "Cannot receive direction", Toast.LENGTH_LONG).show();
					}

			};
		}.execute("...");
	}
	

	
	
	public void updateListToMapView(){
		//Get mapview and set ZoomControl
		mapView = (MapView)findViewById(R.id.mapViewDirectionView);
		mapView.setBuiltInZoomControls(true);
		mapView.setTraffic(true);


		lstOverlay = mapView.getOverlays();       

		for (int j = 0; j < listDetailsPosition.size()-1; j++) {
			lstOverlay.add(new MyOverlay(listDetailsPosition.get(j), listDetailsPosition.get(j+1)));
		}

		//Animate to Start Position
		MapController mc = mapView.getController();
		mc.animateTo(listDetailsPosition.get(0));
		mc.setZoom(zoomLevel);
		mapView.invalidate();	

		List<Overlay> mapOverlays = mapView.getOverlays();

		//Start Marker
		mapOverlays.add(drawMarkerOnMap(listDetailsPosition.get(0), R.drawable.start_marker, "Your current location"));

		//End Marker
		String stNotification = "Name:"+placeReceived.getName()+"\nAddress:"+placeReceived.getAddressLine();
		mapOverlays.add(drawMarkerOnMap(listDetailsPosition.get(listDetailsPosition.size()-1), R.drawable.end_marker, stNotification));
	}
	




}
